package com.xiaotu.spider.project.pageprocessor.list;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.xiaotu.common.exception.SpiderException;
import com.xiaotu.common.exception.SpiderExceptionCode;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.DateUtils;
import com.xiaotu.common.util.FileUtil;
import com.xiaotu.common.util.PageUtils;
import com.xiaotu.common.util.PropertiesUtil;
import com.xiaotu.common.util.SepratorUtil;
import com.xiaotu.spider.AUrlPageProcessor;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.selector.Html;

/**
 * @类名 LeTVListPageProcessor
 * @日期 2017年8月10日
 * @作者 王艳龙
 * @功能 乐视网站2015-2017历史热播数据爬取
 */

@Component("LeTVListPageProcessor")
@Scope("prototype")
public class LeTVListPageProcessor extends AUrlPageProcessor{
	
	private String fileName; //存放文件路径
	private static final String[] DATE_ARR = DateUtils.getRecentYears(3);// 历史热播上映日期最近三年
	
	@Override
	protected String[] getStartRequestUrls()
	{
		int pageSize = PropertiesUtil
				.getPropertyByInt(Constants.SpiderConfigKey.HOTTV_PAGE_SIZE);
		
		String[] urls = new String[DATE_ARR.length * pageSize];
		
		int idx = 0;
		for (String movieDate : DATE_ARR)
		{
			for (int i = 1; i <= pageSize; i++)
			{
				urls[idx] ="http://list.le.com/apin/chandata.json?c=1&d=1&md=&o=3&p="+i+"&s=5&y="+movieDate;
				idx++;
			}
		}
		return urls;
	}
	
	 @Override
	    public void initRunParams(Map<String, Object> map)
	    {
	        if (map.get("fileName") == null)
	            throw new SpiderException(
	                    SpiderExceptionCode.PageProcessorParameterErrorException);
	        fileName = map.get("fileName") + StringUtils.EMPTY;
	    }
	
	@Override
	public String getPageDataType(Page page){
		String url = page.getUrl().toString();
        if (url.contains("http://list.le.com/apin")){
        	 return Constants.PageDataType.SEARCH_LIST;
        }else if(url.contains("/movie")){
        	 return Constants.PageDataType.TV_RECORD;
        }else if(url.contains("/ptv/vplay")){
        	 return Constants.PageDataType.TV_PRO_COMMENT;
        }else{
        	 return Constants.PageDataType.SEARCH_LIST;
        }
        
	}
	
	@Override
	public void process(Page page){
		Map<String, Object> tvMap = page.getRequest().getExtras();
		switch (this.getPageDataType(page)){
            case Constants.PageDataType.SEARCH_LIST:
                this.searchProcess(page);
                break;
            case Constants.PageDataType.TV_RECORD:
            	this.detailsProcess(page,tvMap);
            	break;
            case Constants.PageDataType.TV_PRO_COMMENT:
                this.detailsPlayProcess(page,tvMap);
                break;
            default:
                break;
	     }
	}
	
	public void searchProcess(Page page){
		String json = page.getRawText();
		Map<String, Object> tvMap = page.getRequest().getExtras();
		if (StringUtils.isEmpty(json))
			throw new SpiderException(
					SpiderExceptionCode.DataInvalideException);
		
		JSONObject jsonObj = JSONObject.fromObject(json);
		if (jsonObj == null || jsonObj.isEmpty() || !jsonObj.has("data_list"))
			throw new SpiderException(
					SpiderExceptionCode.DataInvalideException);
		JSONArray items = jsonObj.getJSONArray("data_list");
		for (int i = 0; i < items.size(); i++){
			JSONObject tvJson = items.getJSONObject(i);
			String vids =tvJson.getString("vids");
			if(StringUtils.isNotEmpty(vids)){
				String[] s = vids.split(",");
				String tvIds =s[0];
				String links = "http://www.le.com/ptv/vplay/" + tvIds+ ".html";
				tvMap.put("linkUrl", links);
				tvMap.put("numberBg", tvJson.getString("duration"));
				PageUtils.addTargetRequest(page, links, page.getRequest().getExtras());
			}
		}
	}
	
	//电影详情播放页
	public void detailsPlayProcess(Page page,Map<String, Object> tvMap){
		String links = page.getHtml().xpath("//div[@class='Info']/ul/li[@class='li_09']/em/a/@href").get();
		PageUtils.addTargetRequest(page, links, page.getRequest().getExtras());
	}
	
	//电影查看更多详情页
	public void detailsProcess(Page page,Map<String, Object> tvMap){
		StringBuffer buffer = new StringBuffer();
		//网大名称
		String tvName = page.getHtml()
				.xpath("//div[@class='play_name']/h2/text()").get();
		buffer.append(tvName);
		
		List<String> movieList =page.getHtml().xpath("//div[@class='play_info']/ul/li").all();
		for (String str : movieList) {
			Html htmlStr =new Html(str);
			String spanType = htmlStr.xpath("//li/b/text()").get();
			if(spanType.contains("年代")){
				String movieDate =htmlStr.xpath("//li/span/a/text()").get();
				buffer.append(SepratorUtil.SEP_PIPE+movieDate);
				
				String movieTime =movieDate.substring(0,4);
				buffer.append(SepratorUtil.SEP_PIPE+movieTime);
				//分钟数
				String numberBg =(String)tvMap.get("numberBg");
				buffer.append(SepratorUtil.SEP_PIPE+numberBg);
				
			}else if(spanType.contains("国家/地区")){
				StringBuffer sub = new StringBuffer();
				List<String> movieAreas = htmlStr.xpath("//li/span/a/text()").all();
				for (String areaStr : movieAreas) {
					if(movieAreas.size()>1){
						sub.append(areaStr+SepratorUtil.SEP_SPACE_EN);
					}else{
						sub.append(areaStr);
					}
				}
				String movieArea =sub.toString();
				buffer.append(SepratorUtil.SEP_PIPE+movieArea);
						
			}else if(spanType.contains("类型")){
				StringBuffer sub = new StringBuffer();
				List<String> movieTypes = htmlStr.xpath("//li/span/a/text()").all();
				for (String typeStr : movieTypes) {
					if(movieTypes.size()>1){
						sub.append(typeStr+SepratorUtil.SEP_SPACE_EN);
					}else{
						sub.append(typeStr);
					}
				}
				String movieType =sub.toString();
				buffer.append(SepratorUtil.SEP_PIPE+movieType);
			}
		}
		
		//总播放量
		String playCount =page.getHtml().xpath("//span[@class='play_time']/text()").get();
		if(StringUtils.isNotEmpty(playCount)){
			playCount =playCount.replace("总播放量： ", "").trim();
			buffer.append(SepratorUtil.SEP_PIPE+playCount);
		}
		//评分
		String movieScore =page.getHtml().xpath("//span[@class='sorce_num']/text()").get();
		buffer.append(SepratorUtil.SEP_PIPE+movieScore);
		//简介
		String moviePresent = page.getHtml().xpath("//div[@class='play_intro']/p/text()").get();
		if(StringUtils.isNotEmpty(moviePresent)){
			moviePresent =moviePresent.replace("简介：", "").trim();
			buffer.append(SepratorUtil.SEP_PIPE+moviePresent);
		}
		//网址
		buffer.append(SepratorUtil.SEP_PIPE+tvMap.get("linkUrl"));
		
		//输出csv文件
		FileUtil.writeFile(fileName, buffer.toString()
	                + SepratorUtil.LINE_SEPRATOR);
	}
	
	@Override
	protected String getSpiderType()
	{
		return "LeTVList";
	}
}
